/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.can.bc;

import edu.upn.sigemfi.can.be.Servicio;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Programador
 */
public class ServicioDelegate {

    private ServicioFacadeLocal local;

    public ServicioDelegate(){
        local=lookupServicioFacade();
    }
    
    public void crear(Servicio servicio) throws Exception{
        try {
            local.create(servicio);
        } catch (Exception e) {
            local=lookupServicioFacade();
            throw new Exception("Crear Servicio" + e.getMessage(), e);
        }
    }

    public void editar(Servicio servicio) throws Exception{
        try {
            local.edit(servicio);
        } catch (Exception e) {
            local=lookupServicioFacade();
            throw new Exception("Editar Servicio" + e.getMessage(), e);
        }
    }

    public void eliminar(Servicio servicio) throws Exception{
        try {
            local.remove(servicio);
        } catch (Exception e) {
            local=lookupServicioFacade();
            throw new Exception("Eliminar Servicio" + e.getMessage(), e);
        }
    }

    public Servicio buscar(long codigo) throws Exception{
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local=lookupServicioFacade();
            throw new Exception("Buscar Servicio" + e.getMessage(), e);
        }
    }

    public List<Servicio> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupServicioFacade();
            throw new Exception("Listar Servicio. " + e.getMessage(), e);
        }
    }

    private ServicioFacadeLocal lookupServicioFacade() {
        try {
            Context c = new InitialContext();
            return (ServicioFacadeLocal) c.lookup("java:comp/env/ServicioFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }



}
